<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('项目立项（未立项）')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>

      <template v-slot:itemStatus="scope">
        <span class="badge badge-wrap" :class="`badge-${scope.row.itemStatusStyle}`">
          {{ scope.row.itemStatusLabel }}
        </span>
      </template>

      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <approve-edit-dialog
      :visible.sync="approveVisible"
      @success="handleSuccess"
      :queryId="editId"
    ></approve-edit-dialog>
    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>
    <item-view :visible.sync="viewVisible" :related-data="relatedData"></item-view>
    <credit-card-dialog
      :visible.sync="fileVisible"
      :query-id="queryId"
      @save="fileVisible = false"
    ></credit-card-dialog>
  </div>
</template>

<script>
import SelectAddress from '@/components/select/SelectAddress'
import ApproveEditDialog from '@/credit/views/county/unionProvince/components/ApproveEditDialog'

import AttachCmpt from '@/credit/views/city/program/Attach'
import taskTableMixin from '@/credit/mixin/self/taskTable'

import { enumGuatype, enumItemStatus } from '@/credit/plugins/enumTown'
import ItemView from '@/credit/views/city/self/components/viewfile/ItemView'
import CreditCardDialog from '@/credit/components/creditFile/CreditCardDialog'
export const prelimTableColumns = [
  {
    attrs: { label: '主体名称', prop: 'customer_name', minWidth: '120px' },
    process: (v, row) => ({
      attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
    }),
  },
  { attrs: { label: '业务类型', prop: 'guaType', align: 'center' } },
  { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
  { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
  { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
  { attrs: { label: '申请金额(万元)', prop: 'apply_funds', align: 'center' } },
]

export default {
  components: {
    SelectAddress,
    ApproveEditDialog,
    AttachCmpt,
    ItemView,
    CreditCardDialog,
  },
  mixins: [taskTableMixin],
  props: {},
  data() {
    return {
      attachVisible: false,
      attachRelatedId: '',
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        corp_name: '',
        address_code: '',
        'is_approval.eq': 1,
        'gua_type.eq': 2,
        'item_status.eq': 0,
        'confuse_status.neq': 3,
      },
      order: {
        approved_date: 'DESC',
        create_time: 'DESC',
      },
      tableColumns: [
        ...prelimTableColumns,
        { attrs: { label: '操作时间', prop: 'acceptTime', align: 'center' } },
        { attrs: { label: '项目来源', prop: 'submit_person_name', align: 'center' } },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '项目状态', prop: 'itemStatusLabel', align: 'center' },
          slot: 'itemStatus',
        },
        {
          attrs: { label: '操作', align: 'center', minWidth: 170 },
          operations: [
            {
              content: '项目立项',
              icon: 'fa fa-gavel',
              event: this.handlePrelim,
              ifRender: row => row.res_a === this.userId || row.res_b === this.userId,
            },
            // { content: '上传附件', icon: 'fa fa-clipboard', event: this.hanleAttach },
            {
              content: '数字档案卡',
              icon: 'fa fa-file-text',
              event: this.handleFile,
            },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.handleView,
            },
          ],
        },
      ],

      // 立项
      editId: '',
      approveVisible: false,

      // 档案卡
      queryId: '',
      fileVisible: false,
      viewMode: false,

      relatedData: {},
      viewVisible: false,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    handleFile(row) {
      this.fileVisible = true
      this.queryId = row.customer_related_id
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.acceptTime = this.$utils.dateFormat(item.approved_date || item.create_time, 'yyyy-MM-dd')
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      const itemStatus = enumItemStatus[item.item_status] || {}
      item.itemStatusLabel = itemStatus.label
      item.itemStatusStyle = itemStatus.css

      item.submitType = +item.submit_type
      item.isGovApply = item.submitType === 1

      return item
    },
    handlePrelim(row = {}) {
      this.approveVisible = true
      this.editId = row.customer_related_id
    },
    handleView(row = {}) {
      this.relatedData = { ...row }
      this.viewVisible = true
    },
    handleSuccess() {
      this.$store.dispatch('getTaskCount')
      this.getData()
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
